package com.infore.airsupervise.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.MainDrawerListAdapter;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MyApplication;
import com.infore.airsupervise.bean.AwesomeIconItem;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.service.JobHandleService;
import com.infore.airsupervise.service.MainService;
import com.infore.airsupervise.service.SubService;
import com.infore.airsupervise.ui.activity.AboutActivity;
import com.infore.airsupervise.ui.activity.LoginActivity;
import com.infore.airsupervise.ui.activity.MainActivity;
import com.infore.airsupervise.utils.JumpUtil;
import com.infore.airsupervise.utils.ToastUtil;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;
import com.rey.material.widget.CheckBox;
import com.rey.material.widget.EditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 主界面抽屉Fragment
 */
public class MainDrawerFragment extends BaseFragment {

    @BindView(R.id.head_img)
    ImageView headImg;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.drawer_menu_list)
    ListView menuList;
    private EditText oldPwd, newPwd;
    private CheckBox isShow;


    private SweetAlertDialog dialog;
    private MainActivity parent;

    private MainDrawerListAdapter listAdapter;

    private UserE userE;

    public MainDrawerFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main_drawer, container, false);
        ButterKnife.bind(this, view);
        parent = (MainActivity) getActivity();
        initView();
        return view;
    }

    private void initView() {
        listAdapter = new MainDrawerListAdapter(getContext(), getMenuList());
        menuList.setAdapter(listAdapter);
        menuList.setOnItemClickListener(itemClickListener);
        //用户信息
        if (UserE.getLoginStatus()) {
            userE = UserE.getUserInfo();
            userName.setText(userE.getRealName());
        }
    }

    /**
     * 菜单列表点击事件
     */
    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            switch (i) {
                case 0:     // 修改密码
                    showModifyPwdDialog();
                    break;

                case 1:     // 关于
                    JumpUtil.jumpFromTo(getContext(), AboutActivity.class);
                    break;

                case 2:    //退出登录
                    loginOut();
                    break;
            }
        }
    };

    /**
     * 密码修改请求
     */
    private void modifyPwdDo(final DialogFragment fragment) {
        ToastUtil.showShort(getContext(), "修改密码功能暂未开放，敬请期待！");
    }

    /**
     * 展示密码修改对话框
     */
    private void showModifyPwdDialog() {
        SimpleDialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            protected void onBuildDone(Dialog dialog) {
                dialog.layoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                oldPwd = (EditText) dialog.findViewById(R.id.old_password);
                newPwd = (EditText) dialog.findViewById(R.id.new_password);
                isShow = (CheckBox) dialog.findViewById(R.id.is_show_pwd);
                isShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            oldPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                            newPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        } else {
                            oldPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                            newPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        }
                        //光标位置
                        oldPwd.setSelection(oldPwd.getText().length());
                        newPwd.setSelection(newPwd.getText().length());
                    }
                });
            }

            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                if (oldPwd.getText().length() == 0 || newPwd.getText().length() == 0) {
                    ToastUtil.showShort(getContext(), "密码不能为空~");
                    return;
                }

                // 密码修改处理
                modifyPwdDo(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
            }
        };

        builder.title("修改密码").positiveAction("确认").negativeAction("取消")
                .contentView(R.layout.layout_modify_password);
        DialogFragment fragment = DialogFragment.newInstance(builder);
        fragment.show(getFragmentManager(), null);
    }

    /**
     * 退出登录
     */
    private void loginOut() {
        new SweetAlertDialog(getContext(), SweetAlertDialog.WARNING_TYPE)
                .setTitleText("安全退出当前用户？")
                .setConfirmText("确认")
                .setCancelText("取消")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        // 清除用户信息、登录状态
                        UserE.cleanLoginInfo();
                        UserE.setLoginStatus(false);
                        // 停止位置发送服务
                        getContext().stopService(new Intent(getContext(), SubService.class));
                        getContext().stopService(new Intent(getContext(), MainService.class));

                        // 跳转登录界面
                        JumpUtil.jumpFromTo(parent, LoginActivity.class);
                    }
                }).setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        }).show();
    }

    /**
     * 菜单列表
     *
     * @return
     */
    private List<AwesomeIconItem> getMenuList() {

        AwesomeIconItem modify = new AwesomeIconItem(FontAwesome.FA_UNLOCK_ALT, "修改密码", DefaultBootstrapBrand.INFO, null, R.drawable.password_selector);
        AwesomeIconItem about = new AwesomeIconItem(FontAwesome.FA_COG, "关于", DefaultBootstrapBrand.INFO, null, R.drawable.about_selector);
        AwesomeIconItem logout = new AwesomeIconItem(FontAwesome.FA_SIGN_OUT, "退出登录", DefaultBootstrapBrand.DANGER, null, R.drawable.logout_selector);

        List<AwesomeIconItem> result = new ArrayList<>();
        result.add(modify);
        result.add(about);
        result.add(logout);

        return result;
    }

}
